package cn.udrm.elasticsearch.demo;

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class DemoFile {

    private static String fileTypes="txt,xls,xlsx,doc,docx,ppt,pptx,pdf,ofd";

    private static int threadCounts=16;

    public static void main(String[] args) throws Exception {
        long l= System.currentTimeMillis();
        List list=new CopyOnWriteArrayList();
        File file=new File("/data");
        File[] files=file.listFiles();
        List<File> fileList=new ArrayList<File>();
        for(File f:files){
            if(f.isDirectory()){
                fileList.addAll(Arrays.asList(f.listFiles()));
            }else{
                fileList.add(f);
            }
        }
        if(fileList.size()<=threadCounts){
            threadCounts=fileList.size();
        }

        System.out.println("===========threads："+threadCounts);
        ExecutorService service= Executors.newFixedThreadPool(threadCounts);
        for(File f:fileList){
            service.submit(new Runnable() {
                @Override
                public void run() {
                    //directRecurse(f,list);
                    scanDirNoRecursion(f.getPath(),list);
                }
            });
        }
        service.shutdown();
        boolean flag=true;
        while(flag){
            Thread.sleep(10000);
			System.out.println("==============已检索："+list.size()+",已耗时："+(System.currentTimeMillis()-l));
            if(service.isTerminated()){
                flag=false;
                System.out.println("==============总耗时："+(System.currentTimeMillis()-l)+"，文件量："+list.size());
            }
        }
    }

    //非递归
    public static void scanDirNoRecursion(String path, List totalList){
        LinkedList list = new LinkedList();
        File dir = new File(path);
        File file[] = dir.listFiles();
        for (int i = 0; i < file.length; i++) {
            if (file[i].isDirectory())
                list.add(file[i]);
            else{
                totalList.add(file[i]);
            }
        }
        File tmp;
        while (!list.isEmpty()) {
            tmp = (File)list.removeFirst();//首个目录
            if (tmp.isDirectory()) {
                file = tmp.listFiles();
                if (file == null)
                    continue;
                for (int i = 0; i < file.length; i++) {
                    if (file[i].isDirectory())
                        list.add(file[i]);//目录则加入目录列表，关键
                    else{

                        totalList.add(file[i]);
                    }
                }
            } else {
                totalList.add(tmp);
            }
        }
    }

    public static void directRecurse (File file, List list){
        if(isDirectory(file)){
            File[] files=file.listFiles();
            if(files!=null&&files.length>0){
                for(File f:files){
                    directRecurse(f,list);
                }
            }
        }else{
            list.add(file);
        }
    }

    public static boolean isDirectory(File file){
        if(file.getPath().indexOf(".")>-1){
            return false;
        }else{
            return true;
        }
    }

}
